  DECLARE @Key NVARCHAR(256) = 'lenovo,2008'
  DECLARE ServerCur CURSOR LOCAL LOCAL READ_ONLY
  FOR
    SELECT [Id],[IP],
    CAST(DecryptByPassphrase(@Key,[UserName]) AS NVARCHAR(256)),
    CAST(DecryptByPassphrase(@Key,[Password]) AS NVARCHAR(256))
    FROM [ServerMonitor].[dbo].[Server]
    ORDER BY [Id]
  DECLARE @ServerId INT, @IP VARCHAR(50), @User SYSNAME, @PW SYSNAME
  OPEN ServerCur
  FETCH NEXT FROM ServerCur INTO @ServerId,@IP,@User,@PW
  WHILE @@FETCH_STATUS = 0
  BEGIN    
    DECLARE @query NVARCHAR(MAX) = REPLACE('
    EXEC OPENDATASOURCE(''SQLOLEDB'',''DATA SOURCE=#SourceIP#;USER ID=#User#;PASSWORD=#PW#'').
    msdb.dbo.sp_set_sqlagent_properties 
    @jobhistory_max_rows=100000, 
		@jobhistory_max_rows_per_job=10000','#SourceIP#',@IP)
    SET @query = REPLACE(@query,'#User#',@User)
    SET @query = REPLACE(@query,'#PW#',@PW)
    
    EXEC SP_EXECUTESQL @query  
    
    FETCH NEXT FROM ServerCur INTO @ServerId,@IP,@User,@PW
  END
  CLOSE ServerCur
  DEALLOCATE ServerCur  
  